package com.tionsy.springbootshiro.shiro.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author tiany
 * @date 2018/12/28 13:35
 */
public class JwtHelper {

    private static final String ISSUER = "user";//签发者

    private static final String SECRET = "secretTest";//密钥

    private static final Long TTLMillis = 12 * 60 * 60 * 1000L;//过期时间12小时

    public static final String USERNAME = "username";

    /**
     * 创建jwt
     *
     * @param username
     * @return
     */
    public static String createJWT(String username) {
        Map<String, Object> claims = new HashMap<>();
        claims.put(USERNAME, username);
        Long currentTimeMillis = System.currentTimeMillis();
        JwtBuilder builder = Jwts.builder()
                .setClaims(claims)
                .setIssuer(ISSUER)
                .signWith(SignatureAlgorithm.HS512, SECRET.getBytes())
                .setIssuedAt(new Date(currentTimeMillis))
                .setExpiration(new Date(currentTimeMillis + TTLMillis));
        return builder.compact();
    }

    /**
     * 解析jwt
     *
     * @param jwt
     * @return
     */
    public static Claims parseJWT(String jwt) {
        Claims claims = Jwts.parser()
                .setSigningKey(SECRET.getBytes())
                .parseClaimsJws(jwt)
                .getBody();
        return claims;
    }

}

